using System;
using System.Collections.Generic;
using System.Text;

namespace PointBridge.SPDX
{
    public class CSOutStringBuilder
    {
        #region Constructors
        public CSOutStringBuilder()
        {
            _output = new StringBuilder();
            indent = 0;
        }

        public CSOutStringBuilder(int p_indent)
        {
            _output = new StringBuilder();
            indent = p_indent;
        }
        #endregion

        #region Methods
        public new string ToString()
        {
            return text;
        }
        public void Append(string str)
        {
            _output.Append(strIndent + str);
            lineStart = false;
        }
        private void AppendIndentPlus(string str)
        {
            _output.Append(strIndent + str);
            indent++;
            lineStart = false;
        }
        private void AppendIndentMinus(string str)
        {
            indent--;
            _output.Append(strIndent + str);
            lineStart = false;
        }
        public void AppendLine(string str)
        {
            Append(str + Environment.NewLine);
            lineStart = true;
        }
        public void AppendLineIndentPlus(string str)
        {
            AppendIndentPlus(str + Environment.NewLine);
            lineStart = true;

        }
        public void AppendLineIndentMinus(string str)
        {
            AppendIndentMinus(str + Environment.NewLine);
            lineStart = true;

        }
        public void AppendFormat(string format, params object[] arg0)
        {
            _output.Append(strIndent + string.Format(format, arg0));
            lineStart = false;
        }
        public void AppendLineFormat(string format, params object[] arg0)
        {
            _output.Append(strIndent + string.Format(format, arg0) + System.Environment.NewLine);
            lineStart = true;

        }
        public void AppendFormatIndentPlus(string format, params object[] arg0)
        {
            AppendIndentPlus(strIndent + string.Format(format, arg0));
            lineStart = false;
        }
        public void AppendLineFormatIndentPlus(string format, params object[] arg0)
        {
            AppendIndentPlus(strIndent + string.Format(format, arg0) + Environment.NewLine);
            lineStart = true;
        }
        public void AppendFormatIndentMinus(string format, params object[] arg0)
        {
            AppendIndentMinus(strIndent + string.Format(format, arg0));
            lineStart = false;
        }
        public void AppendLineFormatIndentMinus(string format, params object[] arg0)
        {
            AppendIndentMinus(strIndent + string.Format(format, arg0) + Environment.NewLine);
            lineStart = true;
        }
        #endregion

        #region Accessors and Mutators
        public string text
        {
            get
            {
                return _output.ToString();
            }
        }
        public int indent
        {
            get
            {
                return _indent;
            }
            set
            {
                _indent = value;
                if (_indent < 0) _indent = 0; // throw new PointBridgelEx("Indent may not be negative");
            }
        }
        public string strIndent
        {
            get
            {
                if (lineStart != true) return "";
                StringBuilder ind = new StringBuilder();
                for (int i = 0; i < indent; i++)
                {
                    ind.Append("\t");
                }
                return ind.ToString();
            }
        }
        public bool lineStart
        {
            get
            {
                return _lineStart;
            }
            set
            {
                _lineStart = value;
            }
        }
        #endregion

        #region Internal fields
        private StringBuilder _output;
        private int _indent;
        private bool _lineStart = true;
        #endregion
    }
}
